<template>
	<view class="container ">
		<view class="user-content ">
			<view :style="{ height: CustomBar + 'px' }"></view>
			<view v-if="setting.token.length > 0" class="flex text-center justify-between">
				<view @click="handleUserInfo" class="text-center padding">
					<view class="flex ">
						<view class="image_107">
							<image v-if="info.avatarUrl" class="image_107 round" :src="info.avatarUrl"></image>
							<image v-else class="image_107 round bg-white" src="/static/logo.jpg"></image>
						</view>
						<view class="text-left margin-left-sm">
							<view class="title text-bold text-sigle" style="width: 10rem;">{{ info.nickname||'暂无昵称' }}
							</view>
							<view class="margin-top-xs text-grey light">ID:{{ info.phone||'暂无手机号' }}</view>
						</view>
					</view>
				</view>
				<view @click="handleAddressManage"
					class="right-boxs text-white flex justify-center align-center padding-left  padding-right">
					<text class="text-size-16 text-sigle">地址管理</text>
				</view>
			</view>
			<view v-else class="flex text-center justify-between">
				<view class="text-center padding">
					<view class="flex align-center">
						<view class="">
							<image class="image_107 round bg-white" src="/static/logo.jpg"></image>
						</view>
						<view class="text-left margin-left-sm">
							<view class="title text-bold">登录/注册</view>
							<view class=" text-size-12 text-grey light">立即登录开启源社区之旅</view>
						</view>
					</view>
				</view>
				<view @click="openLoginPopup" class=" text-grey light flex align-center padding-lr"><text
						class="text-size-16">点击登录</text></view>
			</view>

			<view class="padding-lr">
				<view class="member-card">
					<view class="row">
						<navigator class="grid" open-type="navigate" url="/pages/integrals/mall">
							<image src="/static/images/my/me_icon_points.png"></image>
							<!-- 	<view class="value">0</view> -->
							<view class="title">积分商城</view>
						</navigator>
						<navigator class="grid" open-type="navigate" url="/pages/my/coupon">
							<image src="/static/images/my/me_icon_quan.png"></image>
							<!-- <view class="value">0</view> -->
							<view class="title">优惠劵</view>
						</navigator>
						<navigator class="grid" open-type="navigate" url="/pages/my/wallet">
							<image src="/static/images/my/me_icon_wallet.png"></image>
							<!-- <view class="value">0</view> -->
							<view class="title">收益</view>
						</navigator>
						<navigator class="grid" open-type="navigate" url="/pages/gifts/gifts">
							<image src="/static/images/my/me_icon_gift_card.png"></image>
							<!-- <view class="value">0</view> -->
							<view class="title">历史</view>
						</navigator>
					</view>
				</view>
			</view>
		</view>
		<view class="padding-bottom padding-lr">
			<view class="lists padding-xs bg-white">
				<list-cell hover arrow @click="myCode">
					<view class="list-cell-wrapper">
						<view class="title">会员码</view>
						<view class="subtitle">好友邀请得现金奖励，优惠券领不断</view>
					</view>
				</list-cell>
				<list-cell hover arrow @click="handleFleaFair">
					<view class="list-cell-wrapper">
						<view class="title">我的发布</view>
						<view class="subtitle">闲置交易</view>
					</view>
				</list-cell>
				<list-cell hover arrow @click="handleInvitefriends">
					<view class="list-cell-wrapper">
						<view class="title">我的邀请</view>
					</view>
				</list-cell>
				<list-cell hover arrow @click="handleCommentList">
					<view class="list-cell-wrapper">
						<view class="title">我的评价</view>
					</view>
				</list-cell>
				<list-cell hover arrow>
					<view class="list-cell-wrapper">
						<button class="title text-left flex-sub talk-btn" open-type="contact" type="default"
							plain>联系客服</button>
					</view>
				</list-cell>
				<list-cell hover arrow>
					<view class="list-cell-wrapper"><button class="title text-left flex-sub talk-btn" open-type="share"
							type="default" plain>分享好友</button></view>
				</list-cell>
				<list-cell hover arrow @click="handleManage">
					<view class="list-cell-wrapper">
						<view class="title">切换商家</view>
					</view>
				</list-cell>
				<list-cell hover arrow @click="handleEnter">
					<view class="list-cell-wrapper">
						<view class="title">入驻平台</view>
					</view>
				</list-cell>
				<list-cell last arrow @click="handleAbout">
					<view class="list-cell-wrapper">
						<view class="title">关于源社区</view>
					</view>
				</list-cell>
			</view>
		</view>

		<!-- 登录popup -->
		<login-popup :value="visible" @change="handleLogin"></login-popup>
	</view>
</template>

<script setup lang="ts">
	import { ref } from 'vue';
	import listCell from '@/components/list-cell/list-cell.vue';
	import loginPopup from '@/components/login-popup.vue';
	import { onLoad, onPullDownRefresh, onShareAppMessage } from '@dcloudio/uni-app';
	import { useSettingStore } from '@/stores/index';
	import { accountInfo } from '@/common/api/user';
	onShareAppMessage((res) => {
		if (res.from === 'button') {// 来自页面内分享按钮
			console.log(res.target)
		}
		return {
			withShareTicket: false,
			title: info.value.nickname + '邀请您体验小程序',
			content: '源社区吃着更放心',
			imageUrl: '',
			path: '/pages/index/index',
			success: (res : any) => {

			},
			fail: (res : any) => {

			},
			complete: (res : any) => {

			}
		}
	})
	const setting = useSettingStore();
	const CustomBar = setting.CustomBar;
	const loginPopupRef = ref();
	const visible = ref(false);
	const visibleModal = ref('');
	const info = ref({
		"username": null,
		"realName": null,
		"nickname": "",
		"avatarUrl": "",
		"phone": "",
		"address": null,
		"email": null,
		"birthday": null,
		"gender": 1,
		"roleKeys": null,
		"msgNum": "0",
		"surplus": null

	});
	const onUser = async () => {
		if (setting.getToken().length == 0) {
			return
		}
		const res : any = await accountInfo('');
		if (res.code == 1000) {
			info.value = res.data;
		}
	};
	const openLoginPopup = () => {
		if (setting.token.length > 0) {
			return;
		}
		visible.value = true;
	};
	const handleFleaFair=()=>{
		uni.navigateTo({
			url: '/pages/fleaFair/release'
		});
	}
	const handleCommentList=()=>{
		uni.navigateTo({
			url: '/pages/my/myComment'
		});
	}
	const handleLogin = () => {
		visible.value = false;
		onUser();
	};
	const handleInvitefriends = () => {
		uni.navigateTo({
			url: '/pages/my/invitefriends'
		});
	}
	onLoad((option : any) => {
		onUser();
	});
	onPullDownRefresh(() => {
		onUser();
	})
	const handleUserInfo = () => {
		uni.navigateTo({
			url: '/pages/my/info'
		});
	};
	const handleAddressManage = () => {
		uni.navigateTo({
			url: '/pages/addresses/addresses'
		});
	};
	const taskCenter = () => {
		uni.navigateTo({
			url: '/pages/task-center/task-center'
		});
	};
	const openMember = () => {
		uni.navigateTo({
			url: '/pages/my/member'
		});
	};
	const myCode = () => {
		uni.navigateTo({
			url: '/pages/my/code'
		});
	};
	const openBenefits = () => {
		if (setting.isLogin) {
			uni.navigateTo({
				url: '/pages/my/benefits'
			});
		} else {
			loginPopupRef.value.open();
		}
	};
	const handleAbout = () => {
		uni.navigateTo({
			url: '/pages/other/setting'
		});
	};
	const mShopInfo = ref({
		"id": "",
		"createTime": "",
		"createUserId": "",
		"tenantId": "0",
		"version": 4,
		"deleted": false,
		"communityId": "",
		"shopTypeId": "",
		"thumbnail": "",
		"name": "",
		"address": "",
		"region": "",
		"lon": "104.07984",
		"lat": "30.657457",
		"description": "",
		"businessLicense": "",
		"foodBusinessLicense": "",
		"liaison": "",
		"phone": "",
		"corporationName": "",
		"idCardFront": "",
		"idCardBack": "",
		"businessState": 1,
		"state": 0,
		"isRebate": 0,
		"opinion": "测试",
		"sfShopId": null

	})
	const handleManage = async () => {
		uni.navigateTo({
			url: '/views/login/phonelogin'
		});
		// const res : any = await myshop({});
		// if (res.code == 1000 && res.data) {
		// 	mShopInfo.value = res.data;
		// 	uni.navigateTo({
		// 		url: '/manage/index/manager?id=' + mShopInfo.value.id
		// 	})
		// } else {
		// 	uni.navigateTo({
		// 		url: '/views/login/phonelogin'
		// 	});
		// }
	};
	const handleEnter = () => {
		uni.navigateTo({
			url: '/manage/enter/enter'
		});
	}
	const handleChange = (index : any) => {
		visibleModal.value = '';
		uni.navigateTo({
			url: '/manage/index/index?type=' + index
		});
	};
</script>

<style lang="scss" scoped>
	/* #ifdef H5 */
	page {
		height: auto;
		min-height: 100%;
	}

	/* #endif */
	.user-content {
		background-image: url(~@/static/images/my/bg_user_box.jpg);
		background-size: 100% 100%;
	}

	.right-boxs {
		background-image: url(~@/static/images/my/bg_address.png);
		background-size: 100% 100%;
		width: 120px;
		height: 40px;
		text-align: center;
	}

	.lists {
		background: #ffffff;
		box-shadow: 0px 2px 4px 0px rgba(220, 220, 220, 0.5);
		border-radius: 8px;
	}

	.content {
		padding: 0 30rpx;
	}

	.welcome {
		position: relative;
		margin-top: -136rpx;
		display: flex;
		flex-direction: column;
		font-size: $font-size-lg;
		color: $text-color-warning;
	}

	.member-card {
		background-color: $bg-color-white;
		padding: 20rpx;
		display: flex;
		flex-direction: column;
		border-radius: $border-radius-base;
		margin-bottom: 40rpx;
		box-shadow: 0px 2px 4px 0px rgba(220, 220, 220, 0.5);
		border-radius: 8px;

		.info {
			position: relative;
			margin-top: -50rpx;
			display: flex;
			align-items: center;
			position: relative;
			padding: 20rpx 0;
			border-bottom: 1rpx solid rgba($color: $border-color, $alpha: 0.3);

			.title {
				flex: 1;
				font-size: 40rpx;
				color: $text-color-base;
				display: flex;
				align-items: center;

				.tips {
					margin-left: 10rpx;
					font-size: $font-size-sm;
					color: $text-color-assist;
					background-color: #e9e9e9;
					padding: 10rpx 30rpx;
					border-radius: 50rem !important;
					display: flex;
					align-items: center;

					image {
						width: 20rpx;
						height: 20rpx;
					}
				}
			}

			.avatar {
				width: 150rpx;
				height: 150rpx;
				border-radius: 100%;
			}

			.badage {
				font-family: 'neutra';
				position: absolute;
				bottom: 20rpx;
				left: 0;
				border: 2rpx solid $text-color-base;
				padding: 2rpx 20rpx;
				border-radius: $border-radius-lg;
				font-size: $font-size-base;
			}
		}

		.row {
			margin-top: $spacing-row-base;
			width: 100%;
			display: flex;
			align-items: center;

			.grid {
				flex: 1;
				flex-shrink: 0;
				display: flex;
				flex-direction: column;
				align-items: center;

				image {
					width: 100rpx;
					height: 100rpx;
				}

				.value {
					font-family: 'neutra';
					margin-bottom: $spacing-col-sm;
					font-size: $font-size-lg;
				}

				.title {
					font-size: $font-size-sm;
					color: $text-color-grey;
				}
			}
		}
	}

	.xinqiubobao {
		width: 100%;
		position: relative;

		.title {
			margin: 10rpx 0;
			font-size: $font-size-lg;
			font-weight: 500;
		}

		.swiper {
			height: 200rpx;
			margin-bottom: 10rpx;

			.swiper-item {
				.swiper-item-wrapper {
					display: flex;
					background-color: $bg-color-white;
					padding: 40rpx 60rpx;
					border-radius: $border-radius-base;
					align-items: center;
					margin-right: 40rpx;

					image {
						width: 100rpx;
						height: 100rpx;
						border-radius: 100%;
						margin-right: 20rpx;
					}

					.desc {
						display: flex;
						flex-direction: column;

						.title {
							font-size: $font-size-medium;
							font-weight: 500;
							margin-bottom: 10rpx;
						}

						.desc {
							font-size: $font-size-sm;
							color: $text-color-grey;
						}
					}
				}
			}
		}
	}

	.task-center {
		margin: 40rpx 0;
		background-color: $bg-color-white;
		padding: 10rpx 0;
		display: flex;
		align-items: center;
		border-radius: $border-radius-lg;

		.intro {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.title {
				font-size: $font-size-lg;
				color: $text-color-base;
			}

			.subtitle {
				font-family: 'neutra';
				font-size: $font-size-sm;
			}
		}

		.image-wrapper {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;

			image {
				width: 200rpx;
				height: 200rpx;
			}
		}
	}

	.open-gift {
		width: 100%;
		background-color: $bg-color-white;
		padding: 30rpx 40rpx;
		margin-bottom: 20rpx;

		.header-cell {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;

			.title {
				font-size: $font-size-lg;
				font-weight: 500;
			}

			.subtitle {
				font-size: $font-size-base;
				color: $text-color-grey;
			}
		}

		.row {
			display: flex;
			flex-wrap: wrap;

			.grid {
				width: 33.3333%;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20rpx;

				image {
					width: 70rpx;
					height: 70rpx;
					margin-bottom: $spacing-row-base;
				}

				.title {
					font-size: $font-size-base;
					color: $text-color-base;
					display: flex;
					align-items: baseline;

					.number {
						margin-left: 5rpx;
						font-family: 'neutra';
						color: $color-warning;
					}
				}
			}
		}
	}

	.list-cell-wrapper {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.title {
			font-size: $font-size-lg;
		}

		.subtitle {
			font-size: $font-size-sm;
			color: $text-color-assist;
		}

		button {
			border: none;
			padding: 0;
		}
	}
</style>